package vn.com.vhc.babyui;

import vn.com.vhc.json.UserFunctions;
import vn.com.vhc.library.Alert;
import vn.com.vhc.library.Progress;
import vn.com.vhc.library.SimpleCrypto;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends Activity {

	EditText edUserName;
	EditText edPassword;
	Button btnRegister;
	TextView tvForgetPass;

	String strName = "";
	String strPass = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		edUserName = (EditText) findViewById(R.id.txtUserName2);
		edPassword = (EditText) findViewById(R.id.txtPassword2);

	}

	public void btnLogin2(View v) {
		strName = edUserName.getText().toString();
		strPass = edPassword.getText().toString();
		if (checkLogin(strName, strPass)) {
			try {
				strPass = new SimpleCrypto().encrypt("key", strPass);
				String temp = new UserFunctions().loginUser(strName, strPass);
				Intent i = new Intent(getApplicationContext(),
						DialogActivity.class);
				
				if (temp.contains("true"))
				{
					
					i.putExtra("INFO", "SUCCESS");
					i.putExtra("TAG", "LOGIN");
					startActivity(i);
				}
					
				else
				{
					i.putExtra("INFO", "FAIL");
					i.putExtra("TAG", "LOGIN");
					
				}
				startActivity(i);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	protected void onStart() {
		super.onStart();
		btnRegister = (Button) findViewById(R.id.btnRegister2);
		btnRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(getApplicationContext(), Baby.class);
				startActivity(i);
			}
		});
	}
	public boolean checkLogin(String username, String password) {
		if (username.equalsIgnoreCase("") || password.equalsIgnoreCase("")) {
			new Alert()
					.AlertInfo(Login.this, "Lỗi", "Bạn nhập thiếu thông tin");
			return false;
		}

		return true;
	}
}
